{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 交易数据\n",
    "\n",
    "交易类数据提供股票的交易行情数据，通过简单的接口调用可获取相应的DataFrame格式数据，主要包括以下类别：\n",
    "\n",
    "    历史行情数据\n",
    "    复权历史数据\n",
    "    实时行情数据\n",
    "    历史分笔数据\n",
    "    实时报价数据\n",
    "    当日历史分笔\n",
    "    大盘指数列表\n",
    "    大单交易数据\n",
    "\n",
    "历史行情\n",
    "在新版0.5.6中，已经新增了一个接口：get_k_data，具体使用方法请参考tushare公众号文章《全新的免费行情数据接口》 ，建议使用全新接口。\n",
    "\n",
    "获取个股历史交易数据（包括均线数据），可以通过参数设置获取日k线、周k线、月k线，以及5分钟、15分钟、30分钟和60分钟k线数据。本接口只能获取近3年的日线数据，适合搭配均线数据进行选股和分析，如果需要全部历史数据，请调用下一个接口get_h_data()。\n",
    "\n",
    "参数说明：\n",
    "\n",
    "    code：股票代码，即6位数字代码，或者指数代码（sh=上证指数 sz=深圳成指 hs300=沪深300指数 sz50=上证50 zxb=中小板 cyb=创业板）\n",
    "    start：开始日期，格式YYYY-MM-DD\n",
    "    end：结束日期，格式YYYY-MM-DD\n",
    "    ktype：数据类型，D=日k线 W=周 M=月 5=5分钟 15=15分钟 30=30分钟 60=60分钟，默认为D\n",
    "    retry_count：当网络异常后重试次数，默认为3\n",
    "    pause:重试时停顿秒数，默认为0\n",
    "\n",
    "返回值说明：\n",
    "\n",
    "    date：日期\n",
    "    open：开盘价\n",
    "    high：最高价\n",
    "    close：收盘价\n",
    "    low：最低价\n",
    "    volume：成交量\n",
    "    price_change：价格变动\n",
    "    p_change：涨跌幅\n",
    "    ma5：5日均价\n",
    "    ma10：10日均价\n",
    "    ma20:20日均价\n",
    "    v_ma5:5日均量\n",
    "    v_ma10:10日均量\n",
    "    v_ma20:20日均量\n",
    "    turnover:换手率[注：指数无此项]\n",
    "\n",
    "调用方法：\n",
    "```\n",
    "import tushare as ts\n",
    "ts.get_hist_data('600848') #一次性获取全部日k线数据\n",
    "```\n",
    "\n",
    "其他：\n",
    "```\n",
    "ts.get_hist_data('600848', ktype='W') #获取周k线数据\n",
    "ts.get_hist_data('600848', ktype='M') #获取月k线数据\n",
    "ts.get_hist_data('600848', ktype='5') #获取5分钟k线数据\n",
    "ts.get_hist_data('600848', ktype='15') #获取15分钟k线数据\n",
    "ts.get_hist_data('600848', ktype='30') #获取30分钟k线数据\n",
    "ts.get_hist_data('600848', ktype='60') #获取60分钟k线数据\n",
    "ts.get_hist_data('sh'）#获取上证指数k线数据，其它参数与个股一致，下同\n",
    "ts.get_hist_data('sz'）#获取深圳成指k线数据\n",
    "ts.get_hist_data('hs300'）#获取沪深300指数k线数据\n",
    "ts.get_hist_data('sz50'）#获取上证50指数k线数据\n",
    "ts.get_hist_data('zxb'）#获取中小板指数k线数据\n",
    "ts.get_hist_data('cyb'）#获取创业板指数k线数据\n",
    "```\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 复权数据\n",
    "\n",
    "获取历史复权数据，分为前复权和后复权数据，接口提供股票上市以来所有历史数据，默认为前复权。如果不设定开始和结束日期，则返回近一年的复权数据，从性能上考虑，推荐设定开始日期和结束日期，而且最好不要超过三年以上，获取全部历史数据，请分年段分步获取，取到数据后，请及时在本地存储。获取个股首个上市日期，请参考以下方法：\n",
    "\n",
    "df = ts.get_stock_basics()\n",
    "date = df.ix['600848']['timeToMarket'] #上市日期YYYYMMDD\n",
    "\n",
    "本接口还提供大盘指数的全部历史数据，调用时，请务必设定index参数为True,由于大盘指数不存在复权的问题，故可以忽略autype参数。\n",
    "```\n",
    "ts.get_h_data('002337') #前复权\n",
    "ts.get_h_data('002337', autype='hfq') #后复权\n",
    "ts.get_h_data('002337', autype=None) #不复权\n",
    "ts.get_h_data('002337', start='2015-01-01', end='2015-03-16') #两个日期之间的前复权数据\n",
    "\n",
    "ts.get_h_data('399106', index=True) #深圳综合指数\n",
    "```\n",
    "\n",
    "参数说明：\n",
    "\n",
    "    code:string,股票代码 e.g. 600848\n",
    "    start:string,开始日期 format：YYYY-MM-DD 为空时取当前日期\n",
    "    end:string,结束日期 format：YYYY-MM-DD 为空时取去年今日\n",
    "    autype:string,复权类型，qfq-前复权 hfq-后复权 None-不复权，默认为qfq\n",
    "    index:Boolean，是否是大盘指数，默认为False\n",
    "    retry_count : int, 默认3,如遇网络等问题重复执行的次数\n",
    "    pause : int, 默认 0,重复请求数据过程中暂停的秒数，防止请求间隔时间太短出现的问题\n",
    "\n",
    "返回值说明：\n",
    "\n",
    "    date : 交易日期 (index)\n",
    "    open : 开盘价\n",
    "    high : 最高价\n",
    "    close : 收盘价\n",
    "    low : 最低价\n",
    "    volume : 成交量\n",
    "    amount : 成交金额\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 实时行情\n",
    "\n",
    "一次性获取当前交易所有股票的行情数据（如果是节假日，即为上一交易日，结果显示速度取决于网速）\n",
    "\n",
    "import tushare as ts\n",
    "\n",
    "ts.get_today_all()\n",
    "\n",
    "返回值说明：\n",
    "\n",
    "    code：代码\n",
    "    name:名称\n",
    "    changepercent:涨跌幅\n",
    "    trade:现价\n",
    "    open:开盘价\n",
    "    high:最高价\n",
    "    low:最低价\n",
    "    settlement:昨日收盘价\n",
    "    volume:成交量\n",
    "    turnoverratio:换手率\n",
    "    amount:成交量\n",
    "    per:市盈率\n",
    "    pb:市净率\n",
    "    mktcap:总市值\n",
    "    nmc:流通市值\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 历史分笔\n",
    "\n",
    "获取个股以往交易历史的分笔数据明细，通过分析分笔数据，可以大致判断资金的进出情况。在使用过程中，对于获取股票某一阶段的历史分笔数据，需要通过参入交易日参数并append到一个DataFrame或者直接append到本地同一个文件里。历史分笔接口只能获取当前交易日之前的数据，当日分笔历史数据请调用get_today_ticks()接口或者在当日18点后通过本接口获取。\n",
    "\n",
    "参数说明：\n",
    "\n",
    "    code：股票代码，即6位数字代码\n",
    "    date：日期，格式YYYY-MM-DD\n",
    "    retry_count : int, 默认3,如遇网络等问题重复执行的次数\n",
    "    pause : int, 默认 0,重复请求数据过程中暂停的秒数，防止请求间隔时间太短出现的问题\n",
    "\n",
    "调用方法：\n",
    "\n",
    "import tushare as ts\n",
    "\n",
    "df = ts.get_tick_data('600848',date='2014-01-09')\n",
    "df.head(10)\n",
    "\n",
    "返回值说明：\n",
    "\n",
    "    time：时间\n",
    "    price：成交价格\n",
    "    change：价格变动\n",
    "    volume：成交手\n",
    "    amount：成交金额(元)\n",
    "    type：买卖类型【买盘、卖盘、中性盘】\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 实时分笔\n",
    "\n",
    "获取实时分笔数据，可以实时取得股票当前报价和成交信息，其中一种场景是，写一个python定时程序来调用本接口（可两三秒执行一次，性能与行情软件基本一致），然后通过DataFrame的矩阵计算实现交易监控，可实时监测交易量和价格的变化。\n",
    "\n",
    "参数说明：\n",
    "\n",
    "    symbols：6位数字股票代码，或者指数代码（sh=上证指数 sz=深圳成指 hs300=沪深300指数 sz50=上证50 zxb=中小板 cyb=创业板） 可输入的类型：str、list、set或者pandas的Series对象\n",
    "\n",
    "调用方法：\n",
    "```\n",
    "import tushare as ts\n",
    "\n",
    "df = ts.get_realtime_quotes('000581') #Single stock symbol\n",
    "df[['code','name','price','bid','ask','volume','amount','time']]\n",
    "```\n",
    "\n",
    "结果显示：\n",
    "```\n",
    "   code    name     price  bid    ask    volume   amount        time\n",
    "0  000581  威孚高科  31.15  31.14  31.15  8183020  253494991.16  11:30:36\n",
    "```\n",
    "\n",
    "返回值说明：\n",
    "```\n",
    "0：name，股票名字\n",
    "1：open，今日开盘价\n",
    "2：pre_close，昨日收盘价\n",
    "3：price，当前价格\n",
    "4：high，今日最高价\n",
    "5：low，今日最低价\n",
    "6：bid，竞买价，即“买一”报价\n",
    "7：ask，竞卖价，即“卖一”报价\n",
    "8：volume，成交量 maybe you need do volume/100\n",
    "9：amount，成交金额（元 CNY）\n",
    "10：b1_v，委买一（笔数 bid volume）\n",
    "11：b1_p，委买一（价格 bid price）\n",
    "12：b2_v，“买二”\n",
    "13：b2_p，“买二”\n",
    "14：b3_v，“买三”\n",
    "15：b3_p，“买三”\n",
    "16：b4_v，“买四”\n",
    "17：b4_p，“买四”\n",
    "18：b5_v，“买五”\n",
    "19：b5_p，“买五”\n",
    "20：a1_v，委卖一（笔数 ask volume）\n",
    "21：a1_p，委卖一（价格 ask price）\n",
    "...\n",
    "30：date，日期；\n",
    "31：time，时间；\n",
    "```\n",
    "\n",
    "请求多个股票方法（一次最好不要超过30个）：\n",
    "```\n",
    "#symbols from a list\n",
    "ts.get_realtime_quotes(['600848','000980','000981'])\n",
    "#from a Series\n",
    "ts.get_realtime_quotes(df['code'].tail(10))  #一次获取10个股票的实时分笔数据\n",
    "```\n",
    "\n",
    "获取实时指数：\n",
    "\n",
    "```\n",
    "#上证指数\n",
    "ts.get_realtime_quotes('sh')\n",
    "#上证指数 深圳成指 沪深300指数 上证50 中小板 创业板\n",
    "ts.get_realtime_quotes(['sh','sz','hs300','sz50','zxb','cyb'])\n",
    "#或者混搭\n",
    "ts.get_realtime_quotes(['sh','600848'])\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 当日历史分笔\n",
    "\n",
    "获取当前交易日（交易进行中使用）已经产生的分笔明细数据。\n",
    "\n",
    "参数说明：\n",
    "\n",
    "    code：股票代码，即6位数字代码\n",
    "    retry_count : int, 默认3,如遇网络等问题重复执行的次数\n",
    "    pause : int, 默认 0,重复请求数据过程中暂停的秒数，防止请求间隔时间太短出现的问题\n",
    "\n",
    "调用方法：\n",
    "\n",
    "import tushare as ts\n",
    "\n",
    "df = ts.get_today_ticks('601333')\n",
    "df.head(10)\n",
    "\n",
    "返回值说明：\n",
    "\n",
    "    time：时间\n",
    "    price：当前价格\n",
    "    pchange:涨跌幅\n",
    "    change：价格变动\n",
    "    volume：成交手\n",
    "    amount：成交金额(元)\n",
    "    type：买卖类型【买盘、卖盘、中性盘】\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
